import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;

public class StudentEditor extends ItemEditor<Student>
{
    public StudentEditor(Judicator k, Shell parent, String title, Student s)
    {
        super(k,parent,title,s,340,150);
    }

    Label nameLabel, emailLabel, modeLabel;
    Text name, email;
    Combo mode;

    private static final String
        NO_NAME = "Name required.";

    protected void _init()
    {
        nameLabel = new Label(shell, SWT.SHADOW_OUT);
        emailLabel = new Label(shell, SWT.SHADOW_OUT);
        modeLabel = new Label(shell, SWT.SHADOW_OUT);
        name = new Text(shell, SWT.BORDER);
        email = new Text(shell, SWT.BORDER);
        mode = new Combo(shell, SWT.BORDER);

        nameLabel.setText("name:");
        nameLabel.setBounds(20,30,40,30);

        emailLabel.setText("e-mail:");
        emailLabel.setBounds(20,70,40,30);

        modeLabel.setText("format:");
        modeLabel.setBounds(20,110,40,30);

        name.setBounds(60,30,250,20);
        name.setEditable(true);
        email.setBounds(60,70,250,20);
        email.setEditable(true);
        mode.setBounds(60,110,150,20);
        mode.add("html");
        mode.add("plain");
        mode.select(0);

        name.setFocus();
    }

    protected void _assumeData()
    {
        name.setText(target.name);
        email.setText(target.email);
        mode.select(target.preferredFormat);
    }

    protected void _addItem()
    {
        if (name.getText().length() > 0)
        {
            Student s = new Student();
            s.name = name.getText();
            s.email = email.getText();
            s.preferredFormat = mode.getSelectionIndex();
            judicator.addStudent(s);
            close();
        }
        else
        {
            AlertSender.sendAlert(NO_NAME);
        }

    }

    protected void _modifyItem()
    {
        if (name.getText().length() > 0)
        {
            target.name = name.getText();
            target.email = email.getText();
            target.preferredFormat = mode.getSelectionIndex();
            judicator.update(target);
            close();
        }
        else
        {
            AlertSender.sendAlert(NO_NAME);
        }
    }

    protected void _close()
    {
        judicator.mainWindow.studentEditor = null;
    }
}
